package org.career.pdung.validationForm;

import org.career.pdung.form.RegistrationForm;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class RegistrationValidation implements Validator {
  public boolean supports(Class<?> klass) {
    return RegistrationForm.class.isAssignableFrom(klass);
  }

  public void validate(Object target, Errors errors) {
	  RegistrationForm registration = (RegistrationForm) target;
	  
	  ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName","NotEmpty.registration.firstName","First name must not be Empty.");
	  ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName","NotEmpty.registration.lastName","Last name must not be Empty.");
	  ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email","email.registration.email","Email must not be Empty.");
	  ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password","password.registration.email","Password must not be Empty.");
      String firstName = registration.getFirstName();
      if ((firstName.length()) > 20) {
	      errors.rejectValue("firstName","lengthOfFirstName.registration.firstName","First name must between 1 to 20 characters.");
	    }
	  String lastName = registration.getLastName();
      if ((lastName.length()) > 20) {
		      errors.rejectValue("lastName","lengthOfLastName.registration.lastName","Last name must between 1 to 20 characters.");
	   }
      String password = registration.getPassword();
      if ((password.length()) > 8 && password.length() < 20) {
	      errors.rejectValue("password","lengthOfpassword.registration.password","Password must between 8 to 20 characters.");
      }      
      if (!(password).equals(registration.getPasswordConfirm())) {
    	errors.rejectValue("password","matchingPassword.registration.password","Password and Confirm Password Not match.");
      }
  }
}